﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicTacToe
{
    public partial class Game : System.Web.UI.Page
    {
        private Button[,] buttons;
        private static bool gameEnded = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            buttons = new Button[3, 3];
            buttons[0, 0] = Button0;
            buttons[0, 1] = Button1;
            buttons[0, 2] = Button2;
            buttons[1, 0] = Button3;
            buttons[1, 1] = Button4;
            buttons[1, 2] = Button5;
            buttons[2, 0] = Button6;
            buttons[2, 1] = Button7;
            buttons[2, 2] = Button8;


        }

        private string GetWinner()
        {
            for (int i = 0; i < 3; i++)
            {
                if (buttons[i, 0].Text == buttons[i, 1].Text && buttons[i, 1].Text == buttons[i, 2].Text
                    && buttons[i, 0].Text.Length > 0)
                    return buttons[i, 0].Text;
                if (buttons[0, i].Text == buttons[1, i].Text && buttons[1, i].Text == buttons[2, i].Text
                    && buttons[0, i].Text.Length > 0)
                    return buttons[0, i].Text;
            }

            if (buttons[0, 0].Text == buttons[1, 1].Text && buttons[0, 0].Text == buttons[2, 2].Text
                && buttons[0, 0].Text.Length > 0)
                return buttons[0, 0].Text;

            if (buttons[2, 0].Text == buttons[1, 1].Text && buttons[2, 0].Text == buttons[0, 2].Text
                && buttons[2, 0].Text.Length > 0)
                return buttons[2, 0].Text;

            return "";

        }

        protected void cell_Clicked(object sender, EventArgs e)
        {
            
            if (gameEnded) return;
            (sender as Button).Text = "X";


            string winner = GetWinner();
            if (winner.Length > 0)
            {
                moveInfo.InnerText = winner + " wins!";
                gameEnded = true;
                return;
            }


            bool hasPut = false;
            for (int i = 0; i < 3 && !hasPut; i++)
                for (int j = 0; j < 3 && !hasPut; j++)
                {
                    if (buttons[i, j].Text != "X" && buttons[i, j].Text != "O")
                    {
                        buttons[i, j].Text = "O";
                        hasPut = true;
                    }
                }

            winner = GetWinner();
            if (winner.Length > 0)
            {
                moveInfo.InnerText = winner + " wins!";
                gameEnded = true;
            }
        }

        protected void resetGame_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    buttons[i, j].Text = "";

            gameEnded = false;
            moveInfo.InnerText = "";
        }
    }
}